package com.ruoyi.modules.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.modules.domain.AgentLevel;
import com.ruoyi.modules.domain.DyAppUser;
import com.ruoyi.modules.domain.vo.AppDyAgentUserVo;
import com.ruoyi.modules.domain.vo.MoneyRecordDto;
import com.ruoyi.modules.service.IDyAgentUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.modules.mapper.NumRecordMapper;
import com.ruoyi.modules.domain.NumRecord;
import com.ruoyi.modules.service.INumRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 次数记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-03
 */
@Service
@Slf4j
public class NumRecordServiceImpl extends ServiceImpl<NumRecordMapper, NumRecord> implements INumRecordService {

    private final static String Today_free = "todayFree:";
    private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private IDyAgentUserService dyAgentUserService;
    @Autowired
    private RedisCache redisCache;

    @Override
    public boolean addNum(Long agentUserIdId, Long num, String msg) {
        if (0 < baseMapper.addNum(agentUserIdId, num)){
            this.saveRecord(agentUserIdId, 0, num, msg);
            return true;
        }
        return false;
    }

    @Override
    public boolean subNum(Long agentUserId, Long num, String msg) {
        if (0 < baseMapper.subNum(agentUserId, num)){
            this.saveRecord(agentUserId, 1, num, msg);
            return true;
        }
        return false;
    }


    @Override
    @Transactional
    public MoneyRecordDto useNumAndGetId(Long agentUserId, DyAppUser dyAppUser) {
        String dyAppUserNickname = dyAppUser.getNickName();

        AppDyAgentUserVo appDyAgentUserVo = dyAgentUserService.getAppDyAgentUserVo(agentUserId);
        //代理等级免费次数
        Long agentLevelDayNum = appDyAgentUserVo.getAgentLevelDayNum();
        //今天的已经免费的次数
        Long todayFreeNum = appDyAgentUserVo.getTodayFreeNum();
        //消耗次数
        Long consumeNum = 1L;
        String msg = "查看 “" + dyAppUserNickname + "”";

        log.info("数据分析 appDyAgentUserVo ： " + appDyAgentUserVo);
        //如果有代理等级免费次数
        if (agentLevelDayNum != null && todayFreeNum < agentLevelDayNum) {
            consumeNum = 0L;
            Long newTodayFreeNum = todayFreeNum + 1;
            setTodayFreeNum(agentUserId, newTodayFreeNum);
            msg = msg + " [免费 " + newTodayFreeNum  + "/" + agentLevelDayNum +"]";
        }


        if (0 < baseMapper.useNum(agentUserId, consumeNum)){
            NumRecord numRecord = this.saveRecord(agentUserId, 1, consumeNum, msg);
            return MoneyRecordDto.success(numRecord.getId());
        } else {
            return MoneyRecordDto.fail();
        }
    }


    private String getTodayKey(Long agentUserId){
        return Today_free + df.format(new Date()) + ":" + agentUserId;
    }

    @Override
    public Long getTodayFreeNum(Long agentUserId) {
        String key = getTodayKey(agentUserId);
        Long todayFreeNum = redisCache.getCacheObject(key);
        if (todayFreeNum == null){
            todayFreeNum = 1L;
        }
        return todayFreeNum;
    }
    private void setTodayFreeNum(Long agentUserId, Long newVal) {
        String key = getTodayKey(agentUserId);
        redisCache.setCacheObject(key, newVal);
    }


    private NumRecord saveRecord(Long agentUserId, int payType, Long num, String msg){
        NumRecord numRecord = new NumRecord();
        numRecord.setAgentUserId(agentUserId);
        numRecord.setNum(num);
        numRecord.setPayType(payType);
        numRecord.setMsg(msg);
        this.save(numRecord);
        return numRecord;
    }

}
